package com.ssau.expert.core.impl;

import com.ssau.expert.core.api.DatabaseCoreAPI;
import com.ssau.expert.core.model.Rule;
import com.ssau.expert.entity.Ship;
import com.ssau.expert.entity.ShipProfile;

import java.util.*;

/**
 * Author: Sergey Saiyan
 * Created: 05.09.13 13:44
 */
public class DatabaseImpl implements DatabaseCoreAPI {

    private static List<Ship> ships = new ArrayList<>(5);

    private Map<String, Rule> ruleMap = new LinkedHashMap<>();

    @Override
    public List<Ship> getShips() {
        return new ArrayList<>(ships);
    }

    @Override
    public List<ShipProfile> getShipProfiles() {
        return Arrays.asList(ShipProfile.values());
    }

    @Override
    public void putRule(Rule rule) {
        ruleMap.put(rule.getId(), rule);
    }

    @Override
    public Collection<Rule> getRuleCollection() {
        return ruleMap.values();
    }

    @Override
    public void restoreRules(Collection<Rule> rules) {
        ruleMap.clear();
        for (Rule rule : rules) {
            ruleMap.put(rule.getId(), rule);
        }
    }

    @Override
    public void clearRules() {
        ruleMap.clear();
    }

    @Override
    public List<String> getRules() {
        List<String> output = new ArrayList<>();
        for (Rule rule : ruleMap.values()) {
            output.add(rule.getRuleString());
        }
        return output;
    }

    public static DatabaseImpl get() {
        return instance;
    }

    private static DatabaseImpl instance = new DatabaseImpl();

    private DatabaseImpl() {
    }

    // TODO file init
    static {
        ships.add(new Ship(ShipProfile.OCEANOLOGY, "Пётр_Лебедев", 4600, 12000, 50));
        ships.add(new Ship(ShipProfile.OCEANOLOGY, "Академик_Курчатов", 6828, 20000, 84));
        ships.add(new Ship(ShipProfile.HYDRO_BIOLOGY, "Академик_Книпович", 3730, 14000, 27));
        ships.add(new Ship(ShipProfile.HYDRO_BIOLOGY, "Эврика", 3270, 17800, 13));
        ships.add(new Ship(ShipProfile.HYDRO_BIOLOGY, "Одиссей", 3840, 11500, 39));
        ships.add(new Ship(ShipProfile.SEA_BIOLOGY, "Профессор_Богоров", 1600, 10000, 22));
    }
}
